// pages/userCart/userCart.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked:true,
    cart:false,
    gender:1,
    loading:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: '申请用户卡'//页面标题为路由参数
    })
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'member',
          act: 'my_card',
          t_session: session
        },
        success: function (res) {
          var data = res.data;
          if(data.done&&data.retval){
            if (data.retval.real_name!=null){
              that.setData({
                cart: true,
                loading:false,
                user_id:data.retval.user_id,
                name:data.retval.real_name
              })
              wx.setNavigationBarTitle({
                title: '用户卡'//页面标题为路由参数
              })
            }else{
              that.setData({
                loading:false
              })
            }
          }
        }
      })
    })
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  selectAddress:function(){
    var that = this;
    wx.chooseLocation({
      success: function(res) {
        that.setData({
          address:res.address
        })
      },
    })
  },
  name:function(res){
    var that = this;
    that.setData({
      name:res.detail.value
    })
  },
  phone: function (res) {
    var that = this;
    that.setData({
      phone: res.detail.value
    })
  },
  detail: function (res) {
    var that = this;
    that.setData({
      detail: res.detail.value
    })
  },
  checkboxChange:function(res){
    var that = this;
    that.setData({
      checked: that.data.checked==true?false:true
    })
  },
  radioChange:function(res){
    console.log(res);
    var that = this;
    that.setData({
      gender:parseInt(res.detail.value)
    })
  },
  submit:function(res){
    var that = this;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (that.data.name == ""||that.data.name==undefined) {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (that.data.phone == "" || that.data.phone == undefined) {
      wx.showToast({
        title: '请输入手机号！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (that.data.phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (!myreg.test(that.data.phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (that.data.address == "" || that.data.address == undefined) {
      wx.showToast({
        title: '请选择所在地',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (that.data.checked==false){
      wx.showToast({
        title: '请阅读用户需知',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'member',
          act: 'apply_card',
          t_session: session,
          real_name: that.data.name,
          phone_mob: that.data.phone,
          gender: that.data.gender,
          region_name: that.data.address,
          address: that.data.detail
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success:function(res){
          var data = res.data;
          if(data.done){
            that.setData({
              cart:true
            })
            wx.setNavigationBarTitle({
              title: '用户卡'//页面标题为路由参数
            })
            wx.request({
              url: app.api,
              data: {
                app: 'member',
                act: 'my_card',
                t_session: session
              },
              success: function (res) {
                var data = res.data;
                if (data.done && data.retval) {
                  that.setData({
                    user_id: data.retval.user_id,
                    name: data.retval.real_name
                  })
                }
              }
            })
          }else{
            wx.showToast({
              title: '请重试!',
              icon:'loading'
            })
          }
        }
      })
    })
    
    
  }
})